<?php
/**
 * 订单模块数据配置后台 Service
 *
 * 订单模块数据配置后台 Service的注意及说明
 * 以下use模块请不要互相伤害
 *
 * @author    xiecz <xiecz@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Services\Config;
use Log;
use Illuminate\Support\Facades\DB;
use App\Services\Tool\GeneralTool;
use App\Services\Tool\DataBase\SQL;

class AndroidBackstage{

    function __construct(){

    }

    /**
     * 安卓版本列表
     * @param  [type] $input [description]
     * @return [type]        [description]
     */
    function get_android_version_list($input){

        $lists = DB::table('android_versions')
                 ->where(['state' => 1])
                 ->orderBy('created_at','desc')
                 ->paginate(10);
        return $lists;
    }

    /**
     * 安卓版本录入
     * @param  [array] $input [version_name         版本名称
     *                         pakage_size          包大小
     *                         update_describe      更新描述
     *                         download_url         下载地址
     *                         is_must_update       0可选升级  1强制升级
     *                         update_time          更新时间
     *                         version_code         版本号]
     * @return [type]        [description]
     */
    function insert_android_version($input){
        if(!isset($input['version_name']) ||
            !isset($input['pakage_size']) ||
            !isset($input['update_describe'])||
            !isset($input['download_url']) ||
            !isset($input['is_must_update']) ||
            !isset($input['update_time']) ||
            !isset($input['version_code']))
            return GeneralTool::createResult('','M00002');

        $arr['version_name']    = $input['version_name'];
        $arr['pakage_size']     = $input['pakage_size'];
        $arr['update_describe'] = $input['update_describe'];
        $arr['download_url']    = $input['download_url'];
        $arr['is_must_update']  = $input['is_must_update'] == 1 ? 1 : 0;
        $arr['update_time']     = $input['update_time'];
        $arr['version_code']     = $input['version_code'];
        $arr['created_at']      = time();
        $result = SQL::insert(['table'  => 'android_versions',
                               'insert' => $arr]);
        return GeneralTool::createResult('',($result ? 'M00000' : 'M00001'));
    }

    /**
     * 删除安卓版本信息
     * @param  [type] $input [id]
     * @return [type]        [description]
     */
    function deleteAndroidVersion($input){
        if(!isset($input['id'])) return GeneralTool::createResult('','M00002');

        $update = SQL::update(['table' => 'android_versions',
                               'where' => ['id' => $input['id']],
                               'update'=> ['state' => 0]]);
        return $update ? ['state' => 'M00000', 'message' => '删除成功'] :
                         ['state' => 'M00001', 'message' => '该信息已删除，请刷新页面'];
    }

    function test($input){
        return GeneralTool::createResult('','M00000');
    }
}
